home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / content / nsContentPolicyUtils.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-05-08  |  7.7 KB  |  232 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Mozilla code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Zero-Knowledge Systems, Inc.
  19.  * Portions created by the Initial Developer are Copyright (C) 2000
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Timothy Watt <riceman+moz@mail.rit.edu>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  27.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /*
  40.  * Utility routines for checking content load/process policy settings,
  41.  * and routines helpful for content policy implementors.
  42.  */
  43.  
  44. #ifndef __nsContentPolicyUtils_h__
  45. #define __nsContentPolicyUtils_h__
  46.  
  47. // for PR_LOGGING
  48. #include "prlog.h"
  49.  
  50. #include "nsString.h"
  51. #include "nsIContentPolicy.h"
  52. #include "nsIMemory.h"
  53. #include "nsIServiceManager.h"
  54. #include "nsIContent.h"
  55.  
  56. //XXXtw sadly, this makes consumers of nsContentPolicyUtils depend on widget
  57. #include "nsIDocument.h"
  58. #include "nsIScriptGlobalObject.h"
  59.  
  60. #define NS_CONTENTPOLICY_CONTRACTID   "@mozilla.org/layout/content-policy;1"
  61. #define NS_CONTENTPOLICY_CATEGORY "content-policy"
  62. #define NS_CONTENTPOLICY_CID                              \
  63.   {0x0e3afd3d, 0xeb60, 0x4c2b,                            \
  64.      { 0x96, 0x3b, 0x56, 0xd7, 0xc4, 0x39, 0xf1, 0x24 }}
  65.  
  66. /**
  67.  * Evaluates to true if val is ACCEPT.
  68.  *
  69.  * @param val the status returned from shouldProcess/shouldLoad
  70.  */
  71. #define NS_CP_ACCEPTED(val) ((val) == nsIContentPolicy::ACCEPT)
  72.  
  73. /**
  74.  * Evaluates to true if val is a REJECT_* status
  75.  *
  76.  * @param val the status returned from shouldProcess/shouldLoad
  77.  */
  78. #define NS_CP_REJECTED(val) ((val) != nsIContentPolicy::ACCEPT)
  79.  
  80. // Offer convenient translations of constants -> const char*
  81.  
  82. // convenience macro to reduce some repetative typing...
  83. // name is the name of a constant from this interface
  84. #define CASE_RETURN(name)          \
  85.   case nsIContentPolicy:: name :   \
  86.     return #name
  87.  
  88. #ifdef PR_LOGGING
  89. /**
  90.  * Returns a string corresponding to the name of the response constant, or
  91.  * "<Unknown Response>" if an unknown response value is given.
  92.  *
  93.  * The return value is static and must not be freed.
  94.  *
  95.  * @param response the response code
  96.  * @return the name of the given response code
  97.  */
  98. inline const char *
  99. NS_CP_ResponseName(PRInt16 response)
  100. {
  101.   switch (response) {
  102.     CASE_RETURN( REJECT_REQUEST );
  103.     CASE_RETURN( REJECT_TYPE    );
  104.     CASE_RETURN( REJECT_SERVER  );
  105.     CASE_RETURN( REJECT_OTHER   );
  106.     CASE_RETURN( ACCEPT         );
  107.   default:
  108.     return "<Unknown Response>";
  109.   }
  110. }
  111.  
  112. /**
  113.  * Returns a string corresponding to the name of the content type constant, or
  114.  * "<Unknown Type>" if an unknown content type value is given.
  115.  *
  116.  * The return value is static and must not be freed.
  117.  *
  118.  * @param contentType the content type code
  119.  * @return the name of the given content type code
  120.  */
  121. inline const char *
  122. NS_CP_ContentTypeName(PRUint32 contentType)
  123. {
  124.   switch (contentType) {
  125.     CASE_RETURN( TYPE_OTHER      );
  126.     CASE_RETURN( TYPE_SCRIPT     );
  127.     CASE_RETURN( TYPE_IMAGE      );
  128.     CASE_RETURN( TYPE_STYLESHEET );
  129.     CASE_RETURN( TYPE_OBJECT     );
  130.     CASE_RETURN( TYPE_DOCUMENT   );
  131.     CASE_RETURN( TYPE_SUBDOCUMENT);
  132.     CASE_RETURN( TYPE_REFRESH    );
  133.   default:
  134.     return "<Unknown Type>";
  135.   }
  136. }
  137.  
  138. #endif // defined(PR_LOGGING)
  139.  
  140. #undef CASE_RETURN
  141.  
  142. /* Passes on parameters from its "caller"'s context. */
  143. #define CHECK_CONTENT_POLICY(action)                                          \
  144.     nsCOMPtr<nsIContentPolicy> policy =                                       \
  145.          do_GetService(NS_CONTENTPOLICY_CONTRACTID);                          \
  146.     if (!policy)                                                              \
  147.         return NS_ERROR_FAILURE;                                              \
  148.                                                                               \
  149.     return policy-> action (contentType, contentLocation, requestOrigin,      \
  150.                             context, mimeType, extra, decision);
  151.  
  152. /**
  153.  * Alias for calling ShouldLoad on the content policy service.
  154.  * Parameters are the same as nsIContentPolicy::shouldLoad.
  155.  */
  156. inline nsresult
  157. NS_CheckContentLoadPolicy(PRUint32          contentType,
  158.                           nsIURI           *contentLocation,
  159.                           nsIURI           *requestOrigin,
  160.                           nsISupports      *context,
  161.                           const nsACString &mimeType,
  162.                           nsISupports      *extra,
  163.                           PRInt16          *decision)
  164. {
  165.     CHECK_CONTENT_POLICY(ShouldLoad);
  166. }
  167.  
  168. /**
  169.  * Alias for calling ShouldProcess on the content policy service.
  170.  * Parameters are the same as nsIContentPolicy::shouldProcess.
  171.  */
  172. inline nsresult
  173. NS_CheckContentProcessPolicy(PRUint32          contentType,
  174.                              nsIURI           *contentLocation,
  175.                              nsIURI           *requestOrigin,
  176.                              nsISupports      *context,
  177.                              const nsACString &mimeType,
  178.                              nsISupports      *extra,
  179.                              PRInt16          *decision)
  180. {
  181.     CHECK_CONTENT_POLICY(ShouldProcess);
  182. }
  183.  
  184. #undef CHECK_CONTENT_POLICY
  185.  
  186. /**
  187.  * Helper function to get an nsIDocShell given a context.
  188.  * If the context is a document or window, the corresponding docshell will be
  189.  * returned.
  190.  * If the context is a non-document DOM node, the docshell of its ownerDocument
  191.  * will be returned.
  192.  *
  193.  * @param aContext the context to find a docshell for (can be null)
  194.  * @return a WEAK pointer to the docshell, or nsnull if it could
  195.  *     not be obtained
  196.  */
  197. static nsIDocShell*
  198. NS_CP_GetDocShellFromContext(nsISupports *aContext)
  199. {
  200.     if (!aContext) {
  201.         return nsnull;
  202.     }
  203.  
  204.     nsCOMPtr<nsIScriptGlobalObject> scriptGlobal = do_QueryInterface(aContext);
  205.  
  206.     if (!scriptGlobal) {
  207.         // our context might be a document (which also QIs to nsIDOMNode), so
  208.         // try that first
  209.         nsCOMPtr<nsIDocument> doc = do_QueryInterface(aContext);
  210.         if (!doc) {
  211.             // we were not a document after all, get our ownerDocument,
  212.             // hopefully
  213.             nsCOMPtr<nsIContent> content = do_QueryInterface(aContext);
  214.             if (content) {
  215.                 doc = content->GetOwnerDoc();
  216.             }
  217.         }
  218.  
  219.         if (doc) {
  220.             scriptGlobal = doc->GetScriptGlobalObject();
  221.         }
  222.     }
  223.  
  224.     if (!scriptGlobal) {
  225.         return nsnull;
  226.     }
  227.  
  228.     return scriptGlobal->GetDocShell();
  229. }
  230.  
  231. #endif /* __nsContentPolicyUtils_h__ */
  232.